package com.zahb.qadx.ui.popup;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.zahb.qadx.R;
import com.zahb.qadx.model.Category;
import com.zahb.qadx.ui.view.PopupWindowCompat;
import com.zahb.qadx.util.CompatHelper;
import com.zahb.qadx.util.DisplayUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Zdw on 2020/10/19 14:41
 */
public class PopupCurriculumCategory extends PopupWindowCompat {

    private Context mContext;
    private int mLayoutId;
    private List<Category> mList;
    private int mSourceId;
    private Category mAllCategory;
    public String mDefaultStr;

    private CategoryAdapter mAdapterLevel1;
    private CategoryAdapter mAdapterLevel2;
    private CategoryAdapter mAdapterLevel3;

    private onCategorySelectedListener mOnCategorySelectedListener;

    public PopupCurriculumCategory(Context context, @LayoutRes int layoutId, List<Category> list, int sourceId) {
        mContext = context;
        mLayoutId = layoutId;
        mList = list;
        mSourceId = sourceId;
        mAllCategory = new Category();
        mAllCategory.setName("全部");
        mAllCategory.setId(-1);
        mAllCategory.setParentId(-1);
        mDefaultStr = context.getString(R.string.all_curriculum);
        initViews();
    }

    private void initViews() {
        View view = View.inflate(mContext, R.layout.popup_curriculum_category, null);
        setContentView(view);
        initRecyclerViews(view);

        //设置弹出窗体的宽
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置弹出窗体的高
        setHeight(DisplayUtil.dip2px(200f));

        //设置弹出窗体可点击
        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);

        //刷新状态
        update();
        //设置弹出窗体动画
        setAnimationStyle(R.style.PortraitPoPupWindow);
        //设置弹出窗体背景
        setBackgroundDrawable(new ColorDrawable(0x50000000));
    }

    private void initRecyclerViews(View view) {
        TextView tvTitle = view.findViewById(R.id.tv_title);
        tvTitle.setVisibility(View.GONE);
        View divider = view.findViewById(R.id.divider);
        divider.setVisibility(View.GONE);

        RecyclerView recyclerViewLevel1 = view.findViewById(R.id.recycler_view_level1);
        recyclerViewLevel1.setLayoutManager(new LinearLayoutManager(view.getContext()));
        mAdapterLevel1 = new CategoryAdapter(mLayoutId);
        mAdapterLevel1.setNormalColor(Color.parseColor("#ebeef1"));
        recyclerViewLevel1.setBackgroundColor(Color.parseColor("#ebeef1"));
        recyclerViewLevel1.setAdapter(mAdapterLevel1);
        if (mSourceId != -1 && mList != null) {
            for (int i = 0; i < mList.size(); i++) {
                if (mList.get(i).getId() == mSourceId) {
                    mAdapterLevel1.setSelectPos(i + 1);
                    break;
                }
            }
        }
        mAdapterLevel1.setList(mList);
        mAdapterLevel1.addData(0, mAllCategory);
        mAdapterLevel1.setOnItemClickListener((adapter, view1, position) -> {
            if (mAdapterLevel1.getSelectPos() == position && position != 0) return;

            int preSelectPos = mAdapterLevel1.getSelectPos();
            mAdapterLevel1.setSelectPos(position);
            mAdapterLevel1.notifyItemChanged(preSelectPos);
            mAdapterLevel1.notifyItemChanged(position);

            Category category = mAdapterLevel1.getItem(position);
            tvTitle.setText(category.getId() == -1 ? mDefaultStr : category.getName());

            mAdapterLevel2.setSelectPos(0);
            mAdapterLevel2.setList(category.getChildList());
            mAdapterLevel2.addData(0, mAllCategory);

            mAdapterLevel3.setSelectPos(0);
            mAdapterLevel3.setList(new ArrayList<>());
            mAdapterLevel3.addData(0, mAllCategory);

            if (category.getChildList().size() == 0) {
                if (mOnCategorySelectedListener != null) {
                    mOnCategorySelectedListener.onCategorySelected(category);
                    dismiss();
                }
            }
        });

        RecyclerView recyclerViewLevel2 = view.findViewById(R.id.recycler_view_level2);
        recyclerViewLevel2.setLayoutManager(new LinearLayoutManager(view.getContext()));
        mAdapterLevel2 = new CategoryAdapter(mLayoutId);
        mAdapterLevel2.setNormalColor(Color.parseColor("#f7f8fa"));
        recyclerViewLevel2.setBackgroundColor(Color.parseColor("#f7f8fa"));
        recyclerViewLevel2.setAdapter(mAdapterLevel2);
        if (mAdapterLevel1.getSelectPos() > 0) {
            mAdapterLevel2.setList(mAdapterLevel1.getItem(mAdapterLevel1.getSelectPos()).getChildList());
        }
        mAdapterLevel2.addData(0, mAllCategory);
        mAdapterLevel2.setOnItemClickListener((adapter, view1, position) -> {
            if (mAdapterLevel2.getSelectPos() == position && position != 0) return;

            int preSelectPos = mAdapterLevel2.getSelectPos();
            mAdapterLevel2.setSelectPos(position);
            mAdapterLevel2.notifyItemChanged(preSelectPos);
            mAdapterLevel2.notifyItemChanged(position);

            Category category = mAdapterLevel2.getItem(position);
            tvTitle.setText(category.getId() == -1 ? mDefaultStr : category.getName());

            mAdapterLevel3.setSelectPos(0);
            mAdapterLevel3.setList(category.getChildList());
            mAdapterLevel3.addData(0, mAllCategory);
            if (category.getChildList().size() == 0) {
                if (mOnCategorySelectedListener != null) {
                    if (position != 0)// 返回第二列选择项
                        mOnCategorySelectedListener.onCategorySelected(category);
                    else// 返回第一列选择项
                        mOnCategorySelectedListener.onCategorySelected(mAdapterLevel1.getItem(mAdapterLevel1.getSelectPos()));
                    dismiss();
                }
            }
        });

        RecyclerView recyclerViewLevel3 = view.findViewById(R.id.recycler_view_level3);
        recyclerViewLevel3.setLayoutManager(new LinearLayoutManager(view.getContext()));
        mAdapterLevel3 = new CategoryAdapter(mLayoutId);
        mAdapterLevel3.setNormalColor(CompatHelper.getColor(R.color.transparent));
        recyclerViewLevel3.setBackgroundColor(CompatHelper.getColor(R.color.transparent));
        recyclerViewLevel3.setAdapter(mAdapterLevel3);
        mAdapterLevel3.addData(mAllCategory);
        mAdapterLevel3.setOnItemClickListener((adapter, view1, position) -> {
            if (mAdapterLevel3.getSelectPos() == position && position != 0) return;

            int preSelectPos = mAdapterLevel3.getSelectPos();
            mAdapterLevel3.setSelectPos(position);
            mAdapterLevel3.notifyItemChanged(preSelectPos);
            mAdapterLevel3.notifyItemChanged(position);

            Category category = mAdapterLevel3.getItem(position);
            tvTitle.setText(category.getId() == -1 ? mDefaultStr : category.getName());

            if (mOnCategorySelectedListener != null) {
                if (position != 0)// 返回第三列选择项
                    mOnCategorySelectedListener.onCategorySelected(category);
                else if (mAdapterLevel2.getSelectPos() != 0)// 返回第二列选择项
                    mOnCategorySelectedListener.onCategorySelected(mAdapterLevel2.getItem(mAdapterLevel2.getSelectPos()));
                else// 返回第一列选择项
                    mOnCategorySelectedListener.onCategorySelected(mAdapterLevel1.getItem(mAdapterLevel1.getSelectPos()));
                dismiss();
            }
        });
    }

    public interface onCategorySelectedListener {
        void onCategorySelected(Category category);
    }

    public void setOnCategorySelectedListener(onCategorySelectedListener onCategorySelectedListener) {
        mOnCategorySelectedListener = onCategorySelectedListener;
    }

    private static final int padding = DisplayUtil.dip2px(8f);

    private static class CategoryAdapter extends BaseQuickAdapter<Category, BaseViewHolder> {

        private int mSelectPos = 0;
        private int mNormalColor;

        int getSelectPos() {
            return mSelectPos;
        }

        void setSelectPos(int selectPos) {
            mSelectPos = selectPos;
        }

        void setNormalColor(int normalColor) {
            mNormalColor = normalColor;
        }

        CategoryAdapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder baseViewHolder, Category category) {
            TextView textView = baseViewHolder.getView(R.id.text_view);
            textView.setText(category.getName());
            textView.setGravity(Gravity.CENTER);
            textView.setPaddingRelative(padding, 0, padding, 0);
            if (baseViewHolder.getAdapterPosition() == mSelectPos) {
                textView.setTextColor(CompatHelper.getColor(R.color.blue_409eff));
                textView.setBackgroundColor(CompatHelper.getColor(R.color.white_ffffff));
            } else {
                textView.setTextColor(CompatHelper.getColor(R.color.black_303133));
                textView.setBackgroundColor(mNormalColor);
            }
        }
    }
}
